package cn.wizzer.app.web.modules.controllers.sys;

import cn.wizzer.app.sys.modules.models.SysCsAccount;
import cn.wizzer.app.sys.modules.services.SysCsAccountServices;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import java.util.Calendar;
import java.util.List;

/**
 * 客服配置账号
 */
@IocBean
@At("/api/cs/account")
public class ApiSysCsAccountController {

    @Inject
    @Reference
    private SysCsAccountServices sysCsAccountServices;

    /**
     * 获取启用的客户配置账号
     *
     * @return
     */
    @At
    @Ok("json")
    public Result list() {
        List<SysCsAccount> csAccounts = sysCsAccountServices.query(Cnd.where("status", "=", true).orderBy("opAt", "desc"));
        Calendar cal = Calendar.getInstance();
        // 分
        cal.set(Calendar.MINUTE, 0);
        // 秒
        cal.set(Calendar.SECOND, 0);
        for (SysCsAccount csAccount : csAccounts) {
            // 开始时间
            if (null != csAccount.getStartingTime()) {
                cal.set(Calendar.HOUR_OF_DAY, csAccount.getStartingTime());
                csAccount.setStartingTime((int) (cal.getTimeInMillis() / 1000));
            }
            // 结束时间
            if (null != csAccount.getEndTime()) {
                cal.set(Calendar.HOUR_OF_DAY, csAccount.getEndTime());
                csAccount.setEndTime((int) (cal.getTimeInMillis() / 1000));
            }
        }
        return Result.success().addData(csAccounts);
    }

}
